<template>
<el-dialog :title='tags' :visible="showDialog" :close-on-click-modal="false" @close="closeDialog" width="400px" @open="tagsApi">
<el-form :model="form" :rules="rules" ref="refForm">
      <el-form-item label="所属学科" prop="subjectID" size="small">
        <el-select v-model="form.subjectID" style="width:78%" placeholder="请选择">
          <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="标签名称" prop="tagName" size="small">
        <el-input v-model="form.tagName" style="width:78%" placeholder="请输入标签名称" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button  @click="closeDialog" >取消</el-button>
       <el-button type="primary" @click="confirmAddDepts" >确认</el-button>
       <!-- @click="confirmAddDepts" -->
    </div>
</el-dialog>
</template>

<script>
import { update, add } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      form: {
        subjectID: '', // 学科id
        tagName: '' // 标签名称
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: ['blur', 'change'] }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: ['blur', 'change'] }
        ]
      },
      userList: [],
      tags: ''
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      defaule: false
    },
    tagsss: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    closeDialog () {
      this.$refs.refForm.resetFields()
      this.$emit('update:showDialog', false)
      this.tagsss.id = ''
      this.form.tagName = ''
      this.form.subjectID = ''
      this.$parent.tagS()
    },
    // 获取学科
    async getSimpleApi () {
      const { data } = await simple()
      this.userList = data
      // console.log(data)
    },
    // 确定按钮添加
    confirmAddDepts () {
      this.$refs.refForm.validate(async valid => {
        if (!valid) return
        if (this.tagsss.id) {
          await update({ id: this.tagsss.id, subjectID: this.form.subjectID, tagName: this.form.tagName })
        } else { await add(this.form) }
        this.closeDialog()
        this.$message.success('操作成功')
        this.$emit('add-success')
      })
    },
    tagsApi () {
      // console.log(this.tagsss)
      if (this.tagsss.id
      ) {
        this.tags = '修改标签'
        this.form.tagName = this.tagsss.tagName
        this.form.subjectID = this.tagsss.subjectID
      } else {
        this.tags = '新增标签'
      }
    }
  },
  created () {
    this.getSimpleApi()
  }

}
</script>

<style scoped lang='scss'>

</style>
